using Traceability.API.Dtos;
using Traceability.Domain;

namespace Traceability.API.Services
{
    /// <summary>
    /// 溯源信息Redis缓存帮助类
    /// </summary>
    public class TraceabilityRedisHelper
    {
        private readonly RedisService _redisService;
        private const string TraceabilityKeyPrefix = "traceability:info:";
        private const string TraceabilityListKey = "traceability:list";
        private const int DefaultExpiry = 3600; // 默认过期时间1小时

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="redisService">Redis服务</param>
        public TraceabilityRedisHelper(RedisService redisService)
        {
            _redisService = redisService;
        }

        /// <summary>
        /// 获取溯源信息缓存键
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>缓存键</returns>
        private string GetTraceabilityKey(int traceabilityId)
        {
            return $"{TraceabilityKeyPrefix}{traceabilityId}";
        }

        /// <summary>
        /// 缓存溯源信息
        /// </summary>
        /// <param name="traceabilityInfo">溯源信息</param>
        /// <param name="expiry">过期时间（秒）</param>
        /// <returns>是否成功</returns>
        public async Task<bool> CacheTraceabilityInfoAsync(TraceabilityInfo traceabilityInfo, int expiry = DefaultExpiry)
        {
            if (traceabilityInfo == null)
                return false;

            string key = GetTraceabilityKey(traceabilityInfo.TraceabilityId);
            return await _redisService.SetAsync(key, traceabilityInfo, expiry);
        }

        /// <summary>
        /// 获取缓存的溯源信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>溯源信息</returns>
        public async Task<TraceabilityInfo> GetTraceabilityInfoAsync(int traceabilityId)
        {
            string key = GetTraceabilityKey(traceabilityId);
            return await _redisService.GetAsync<TraceabilityInfo>(key);
        }

        /// <summary>
        /// 删除缓存的溯源信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> RemoveTraceabilityInfoAsync(int traceabilityId)
        {
            string key = GetTraceabilityKey(traceabilityId);
            return await _redisService.DeleteAsync(key);
        }

        /// <summary>
        /// 缓存溯源信息列表
        /// </summary>
        /// <param name="traceabilityInfos">溯源信息列表</param>
        /// <param name="expiry">过期时间（秒）</param>
        /// <returns>是否成功</returns>
        public async Task<bool> CacheTraceabilityListAsync(List<TraceabilityInfo> traceabilityInfos, int expiry = DefaultExpiry)
        {
            if (traceabilityInfos == null || !traceabilityInfos.Any())
                return false;

            return await _redisService.SetAsync(TraceabilityListKey, traceabilityInfos, expiry);
        }

        /// <summary>
        /// 获取缓存的溯源信息列表
        /// </summary>
        /// <returns>溯源信息列表</returns>
        public async Task<List<TraceabilityInfo>> GetTraceabilityListAsync()
        {
            return await _redisService.GetAsync<List<TraceabilityInfo>>(TraceabilityListKey);
        }

        /// <summary>
        /// 删除缓存的溯源信息列表
        /// </summary>
        /// <returns>是否成功</returns>
        public async Task<bool> RemoveTraceabilityListAsync()
        {
            return await _redisService.DeleteAsync(TraceabilityListKey);
        }

        /// <summary>
        /// 更新溯源信息缓存
        /// </summary>
        /// <param name="traceabilityInfo">溯源信息</param>
        /// <param name="expiry">过期时间（秒）</param>
        /// <returns>是否成功</returns>
        public async Task<bool> UpdateTraceabilityInfoAsync(TraceabilityInfo traceabilityInfo, int expiry = DefaultExpiry)
        {
            // 先删除旧缓存
            await RemoveTraceabilityInfoAsync(traceabilityInfo.TraceabilityId);
            // 再添加新缓存
            return await CacheTraceabilityInfoAsync(traceabilityInfo, expiry);
        }

        /// <summary>
        /// 判断溯源信息是否存在于缓存中
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>是否存在</returns>
        public async Task<bool> ExistsTraceabilityInfoAsync(int traceabilityId)
        {
            string key = GetTraceabilityKey(traceabilityId);
            return await _redisService.ExistsAsync(key);
        }
    }
} 